package phenix.com.phenixlib.util;

import android.content.Context;
import android.util.Log;

/**
 * 日志输出类
 * 
 * @author zhouphenix
 * 部分level日志不输出问题--解决：进入调试设备开发者选项中>>日志输出>>设置全部日志输出
 */
public class Logger {
	// 控制日志输出标志
	public static boolean DEBUG = false;

	// 日志输出查找标志
	private static String TAG = "[phenix]";

	// 日志输出级别
	private static final int LOG_LEVEL = Log.VERBOSE;



	/**
	 * 默认logger采用应用名作为输出tag(如果期望是这样的tag，应用中调用一次初始化)
	 * 
	 * @param mContext
	 */
	public static void initDefault(Context mContext) {
		/**获取应用名作为标签*/
		TAG = mContext.getPackageManager().getApplicationLabel(mContext.getApplicationInfo()).toString();
	}



	/**
	 * Get The CurrentThread Function Name
	 * 
	 * @return
	 */
	private static String getFuncName() {
		StackTraceElement[] stackElements = Thread.currentThread()
				.getStackTrace();
		if (stackElements == null) {
			return null;
		}
		for (StackTraceElement e : stackElements) {
			if (e.isNativeMethod())
				continue;
			if (e.getClassName().equals(Thread.class.getName()))
				continue;
			if (e.getClassName().equals(Logger.class.getName()))
				continue;
			return e.getClassName() + "->[ThreadID-" + Thread.currentThread().getId() + "]->"
					+ e.getFileName() + "->" + e.getMethodName() + "->"
					+ e.getLineNumber();
		}

		return null;
	}

	public static void d(Object o) {
		if (DEBUG) {
			debug(o);
		}
	}

	public static void d(String tag,Object o){
		if (null != TAG)TAG = tag;
		d(o);
	}

	private static void debug(Object o) {
		if (LOG_LEVEL <= Log.DEBUG) {
			String funcName = getFuncName();
			String oStr = (funcName == null ? o.toString() : funcName + "->"
					+ o);
			Log.d(TAG, oStr);
		}
	}

	public static void i(Object o) {
		if (DEBUG) {
			info(o);
		}
	}
	public static void i(String tag,Object o) {
		if (null != TAG)TAG = tag;
			i(o);
	}

	private static void info(Object o) {
		if (LOG_LEVEL <= Log.INFO) {
			String funcName = getFuncName();
			String oStr = (funcName == null ? o.toString() : funcName + "->"
					+ o);
			Log.i(TAG, oStr);
		}
	}

	public static void v(Object o) {
		if (DEBUG) {
			verbose(o);
		}
	}
	public static void v(String tag,Object o) {
		if (null != TAG)TAG = tag;
			v(o);
	}

	private static void verbose(Object o) {
		if (LOG_LEVEL <= Log.VERBOSE) {
			String funcName = getFuncName();
			String oStr = (funcName == null ? o.toString() : funcName + "->"
					+ o);
			Log.v(TAG, oStr);
		}
	}

	public static void w(Object o) {
		if (DEBUG) {
			warn(o);
		}
	}
	public static void w(String tag,Object o) {
		if (null != TAG)TAG = tag;
		    w(o);
	}

	private static void warn(Object o) {
		if (LOG_LEVEL <= Log.WARN) {
			String funcName = getFuncName();
			String oStr = (funcName == null ? o.toString() : funcName + "->"
					+ o);
			Log.w(TAG, oStr);
		}
	}

	public static void e(Object o) {
		if (DEBUG) {
			error(o);
		}
	}
	public static void e(String tag,Object o) {
		if (null != TAG)TAG = tag;
			e(o);
	}

	private static void error(Object o) {
		if (LOG_LEVEL <= Log.ERROR) {
			String funcName = getFuncName();
			String oStr = (funcName == null ? o.toString() : funcName + "->"
					+ o);
			Log.e(TAG, oStr);
		}
	}

	public void e(String log, Throwable tr) {
		if (DEBUG) {
			Log.e(TAG, "{Thread:" + Thread.currentThread().getName() + "}"
					+ "[" + getFuncName() + "]->" + log + "\n", tr);
		}
	}

	/**
	 * TODO 增加打印本地文件的方法
	 */

}